home *** CD-ROM | disk | FTP | other *** search
- unit calcform;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, XPMan;
-
- type
- TForm1 = class(TForm)
- CalcBtn: TButton;
- GrandTotal: TEdit;
- Vat: TEdit;
- SubTotal: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- procedure CalcBtnClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
-
-
- procedure TForm1.CalcBtnClick(Sender: TObject);
- // all variables must be declared in the VAR section
- // syntax is:
- // < variable name > : < type > ;
- // multiple variables of the same type may
- // be separated by commas
- var
- stxt, vtxt,
- gtxt : string;
- st, vt, gt : real;
- errcode : integer;
- begin
- // First, let's initialise the variables to 0.0 for the
- // floating point numbers and empty strings, '', for the
- // strings
- st := 0.0;
- vt := 0.0;
- gt := 0.0;
- stxt := '';
- vtxt := '';
- gtxt := '';
- begin // in Pascal code blocks are delimited by the keywords
- // BEGIN and END - these are like the curly braces {}
- // in C-like languages and Java
-
- // try to convert SubTotal.Text to the real value, st.
- // if there is an error, errorcode returns the index
- // of the error in the string
- Val(SubTotal.Text, st, errcode);
- // Use errorcode to index into the input string in order to
- // display the problem character
- if errcode <> 0 then
- ShowMessage('Character #'+IntToStr(errcode)+' "'
- +SubTotal.Text[errcode]+'" is invalid.')
- else
- begin
- vt := (st * 0.175);
- gt := vt + st;
- // Instead of the FloatToStr() functions you could use
- // the Str() procedure which allows you to set a # of decimal
- // places - here 2
- // Str(vt:2:2, vtxt);
- // Str(gt:2:2, gtxt);
- vtxt := FloatToStr(vt);
- gtxt := FloatToStr(gt);
- Vat.Text := vtxt;
- GrandTotal.Text := gtxt;
- end
- end
- end;
-
- end.
-